package Z.dailyExercise._0225;

public class _剑指Offer21调整数组顺序使奇数位于偶数前面 {
    /**
     * 双指针：前后指针 ==耗时
     * 前偶后奇 交换 i++ j--;
     * 前奇后偶 i++ j--
     * 前奇数后奇数 i++
     * 前偶数 后偶数 j--;
     * @param nums
     * @return
     */
    public int[] exchange(int[] nums) {

        int i=0,j= nums.length-1;

        while (i<j){
            if (judge(nums[i]) && !judge(nums[j])){
                int temp= nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }

            if (judge(nums[i]) && judge(nums[j])){
                j--;
                continue;
            }

            if (!judge(nums[i]) && !judge(nums[j])){
                i++;
                continue;
            }
            //if (judge(nums[i]) || judge(nums[j])){
            i++;
            j--;
            //}

        }

        return nums;

    }
    boolean judge(int num){
        if (num%2 == 0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 题解：快慢指针法
     * @param nums
     * @return
     */
    public int[] exchange1(int[] nums){
        int fast =0;
        int low =0;

        while (fast<nums.length){

            if (nums[fast] %2 ==0){
                int temp= nums[low];
                nums[low] = nums[fast];
                nums[fast] = temp;
                low++;
            }
            fast++;
        }

        return nums;
    }


}
